package cn.YstAdmin.controller;

import cn.Product.service.ProductService;
import cn.YstAdmin.service.YstAdminService;
import cn.YstHome.service.YstHomeService;
import cn.controller.AopController;
import cn.util.aop.Authority;
import cn.util.json.JacksonUtil;
import common.constant.ServiceResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("52ystqipaadmin")
public class YstAdminController {

    private Logger logger = LoggerFactory.getLogger(AopController.class);


    /**
     * 产品接口类
     */
    @Autowired
    ProductService productService;

    /**
     * 后台
     *
     */
    @Autowired
    YstAdminService ystAdminService;

    /**
     * 首页接口
     */
    @Autowired
    YstHomeService ystHomeService;


    /**
     * 后台管理
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/index")
    @Authority()
    public Object admin(@RequestBody String json, Model model) {
        logger.info("访问地址===admin;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String, Object> result = new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //返回模板
            map.put("path", "admin/index");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    @Authority()
    @RequestMapping("/product-list")
    public Object getadminProductList(@RequestBody String json, Model model, @RequestParam(value = "SPMC", required = false, defaultValue = "") String SPMC, @RequestParam(value = "starttime", required = false, defaultValue = "") String starttime, @RequestParam(value = "endtime", required = false, defaultValue = "") String endtime, @RequestParam(value = "SPLX", required = false, defaultValue = "") String SPLX){
        logger.info("访问地址===52ystqipaadmin/product-list;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String, Object> result = new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            map.put("SPMC", SPMC);
            map.put("starttime", starttime);
            map.put("endtime", endtime);
            map.put("SPLX", SPLX);
            //获取商品
            result.put("ProductList", productService.getadminProductList(map));
            //商品类型
            result.put("ProductType", productService.getProductType(map));
            //回显数据
            result.put("HX", map);
            //返回模板
            map.put("path", "admin/product-list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 进入添加产品 页面
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/product-add")
    @Authority()
    public Object  getadminProductaddYe(@RequestBody String json, Model model,@RequestParam(value = "SPBH",required = false) String SPBH)  {
        logger.info("访问地址===product-add;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String, Object> result = new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //获取商品信息  进行修改
            if(SPBH!=null){
                map.put("SPBH",SPBH);
                result=productService.getProductInfo(map);
            }
            //产品类别
            result.put("ProductType",productService.getProductType(map));
            //返回模板
            map.put("path", "admin/product-add");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 提交修改添加产品
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addadminProduct.do",method=RequestMethod.POST)
    @ResponseBody
    @Authority()
    public Object  addadminProduct(@RequestBody String json, Model model) {
        logger.info("访问地址===addadminProduct.do;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","string");
            if(map.get("SPBH")==null||map.get("SPBH").equals("")){
                //保存商品
                result=productService.addadminProduct(map);
            }else{
                //修改商品
                result=productService.updateadminProduct(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 删除产品
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/daleteadminProduct.do",method=RequestMethod.POST)
    @ResponseBody
    @Authority()
    public Object  daleteadminProduct(@RequestBody String json, Model model) {
        logger.info("访问地址===daleteadminProduct.do;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","string");
            result=productService.daleteadminProduct(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 进入分类
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/product-category")
    @ResponseBody
    @Authority()
    public Object  getadminProductCategory(@RequestBody String json, Model model) {
        logger.info("访问地址===product-category;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            result=productService.getProductType(map);
            //返回模板
            map.put("path", "admin/product-category");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 修改分类
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateadminProductType.do",method=RequestMethod.POST)
    @ResponseBody
    @Authority()
    public Object  updateadminProductType(@RequestBody String json, Model model) {
        logger.info("访问地址===updateadminProductType.do;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","string");
            result=productService.updateadminProductType(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 添加分类
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addadminProductType.do",method=RequestMethod.POST)
    @ResponseBody
    @Authority()
    public Object  addadminProductType(@RequestBody String json, Model model) {
        logger.info("访问地址===addadminProductType.do;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","string");
            result=productService.addadminProductType(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 删除分类
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/daleteadminProductType.do",method=RequestMethod.POST)
    @ResponseBody
    @Authority()
    public Object  daleteadminProductType(@RequestBody String json, Model model) {
        logger.info("访问地址===daleteadminProductType.do;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","string");
            result=productService.daleteadminProductType(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 进入品牌管理
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/product-brand")
    @ResponseBody
    @Authority()
    public Object  getadminProductBrand(@RequestBody String json, Model model) {
        logger.info("访问地址===product-brand;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result =null;
        try {
            map = JacksonUtil.parseJson(json);
            result=productService.getProductType(map);
            //返回模板
            map.put("path", "admin/product-brand");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 进入系统设置
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/system-base")
    @ResponseBody
    @Authority()
    public Object  getadminSystemBase(@RequestBody String json, Model model) {
        logger.info("访问地址===system-base;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //基本信息
            result.put("YstInfo",ystAdminService.getYstInfo(map));
            //阿里云OSS信息
            result.put("YstALiOSS",ystAdminService.getYstALiOSS(map));
            //Redis缓存信息
            result.put("YstRedis",ystAdminService.getYstRedis(map));
            //返回模板
            map.put("path", "admin/system-base");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 修改系统设置
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/system-base.do")
    @ResponseBody
    @Authority()
    public Object  updateadminSystemBase(@RequestBody String json, Model model) {
        logger.info("访问地址===system-base.do;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Object result=null;
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype","string");
            //基本信息
            result=ystAdminService.updateadminSystemBase(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }


    /**
     * 进入后端栏目界面
     * @param json
     * @param model
     * @return
     */
    @RequestMapping(value = "/system-category")
    @ResponseBody
    @Authority()
    public Object  getadminSystemCategory(@RequestBody String json, Model model) {
        logger.info("访问地址===getadminSystemCategory;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //获取导航信息
            result.put("YstInfoNav",ystHomeService.getYstInfoNav(map));
            //返回模板
            map.put("path", "admin/system-category");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 进入角色管理
     */
    @RequestMapping(value = "/admin-role")
    @ResponseBody
    @Authority()
    public Object  getadminRole(@RequestBody String json, Model model) {
        logger.info("访问地址===admin-role;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //获取导航信息
            result.put("YstRole",ystAdminService.getadminRole(map));
            //返回模板
            map.put("path", "admin/admin-role");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 进入权限管理
     */
    @RequestMapping(value = "/admin-permission")
    @ResponseBody
    @Authority()
    public Object  getadminPermission(@RequestBody String json, Model model) {
        logger.info("访问地址===admin-permission;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //获取导航信息
            //result.put("YstInfoNav",ystHomeService.getadminPermission(map));
            //返回模板
            map.put("path", "admin/admin-permission");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }
    /**
     * 进入管理员列表
     */
    @RequestMapping(value = "/admin-list")
    @ResponseBody
    @Authority()
    public Object  getadminList(@RequestBody String json, Model model) {
        logger.info("访问地址===admin-list;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String,Object> result=new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            //获取导航信息
            //result.put("YstInfoNav",ystHomeService.getadminList(map));
            //返回模板
            map.put("path", "admin/admin-list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    /**
     * 上传图片
     *
     * @param json
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload/{imgtype}",method=RequestMethod.POST)
    @Authority("upload")
    public Object  UploadOSS(@RequestBody String json, Model model,@RequestParam("file") MultipartFile[] files,@PathVariable String imgtype) {
        logger.info("访问地址===upload;访问参数===" + json);
        //传过来的数据
        Map<String, Object> map = null;
        //返回的数据
        Map<String, Object> result = new HashMap<>();
        try {
            map = JacksonUtil.parseJson(json);
            map.put("resultype", "json");
            map.put("path", imgtype);
            //图片返回路径
            result.put("imagesurl", ystAdminService.UploadOSS(map, files));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServiceResult.createResult(model, map, result);

    }

    }
